import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"

const routes = [
    { 
        path: '/', 
        // 路由重定向
        redirect: '/home'
    },
    { 
        path: '/home', 
        name: 'home',
        meta: {
            title: '系统首页',
        },
        component: () => import( /*webpackChunkName: 'Home'*/  '@/views/home/index.vue'),
        children: [
            {
                path: "user", 
                name: "user", 
                meta: {
                    title: '用户管理',
                },
                component: () => import("@/views/demo/user/index.vue"),
                children: [
                    {
                        path: 'add',
                        component: () => import('@/views/demo/user/add.vue'),
                    },
                    {
                        path: 'edit',
                        component: () => import('@/views/demo/user/edit.vue'),
                    },
                    {
                        path: 'del',
                        component: () => import('@/views/demo/user/del.vue'),
                    },
                    {
                        path: 'index',
                        component: () => import('@/views/demo/user/index.vue'),
                    },
                ]
            },
            {
                path: "role", 
                name: "role", 
                meta: {
                    title: '角色管理',
                },
                component: () => import("@/views/demo/Role.vue"),
            },
            { 
                path: "demo", 
                name: "Demo", 
                meta: {
                    title: '演示管理',
                },
                component: () => import("@/views/demo/demo.vue") 
            },
            { 
                path: 'setting', 
                name: 'setting',
                meta: {
                    title: '系统设置',
                    noAuth: false 
                },
                component: () => import( /*webpackChunkName: 'Setting'*/  '@/views/demo/Setting.vue')
            },
            { 
                path: 'useInfo', 
                name: 'useInfo',
                meta: {
                    title: '使用说明',
                    noAuth: false 
                },
                component: () => import( /*webpackChunkName: 'UseInfo'*/  '@/views/demo/UseInfo.vue')
            },
        ]
    },
    
    { 
        path: '/login', 
        name: 'login',
        meta: {
            title: '用户登录',
            noAuth: true 
        },
        component: () => import( /*webpackChunkName: 'Login'*/  '@/views/login/index.vue')
    },

    { 
        path: '/404', 
        name: '404',
        meta: {
            title: '404',
            noAuth: true 
        },
        component: () => import( /*webpackChunkName: '404'*/  '@/views/error/404.vue')
    },

    { 
        path: '/401', 
        name: '401',
        meta: {
            title: '401',
            noAuth: true 
        },
        component: () => import( /*webpackChunkName: '401'*/  '@/views/error/401.vue')
    },
    
]

 
const router = createRouter({
    history: createWebHistory(),
    routes,
})

const getToken = ()=> {
    const token = localStorage.getItem('token')
    return !!token
}

router.beforeEach((to, from, next) => {
    if(to.meta && to.meta.noAuth) {
        document.title = to.meta.title ?? '后台管理系统'
        next()
        retrun
    }
    if(getToken()) {
        document.title = to.meta.title ?? '后台管理系统'
        next()
        retrun
    }
    // 第一次登录的用户。
    next({
        name: 'login'
    })
})



export default router